package com.umeox.watch.moto.contact.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.umeox.watch.moto.contact.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Rambo 2019-07-04
 * Describe:
 */
public class SpreadView1 extends View {

    private static int centerColor;
    private Paint mPaint;
    private float alpharate;
    private int mSpace;
    private int mWidth;
    private List<Integer> mAlphas = new ArrayList<>();
    private List<Float> widths = new ArrayList<>();
    // 是否运行
    private boolean isStarting = false;


    public SpreadView1(Context context) {
        this(context, null);
    }

    public SpreadView1(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public SpreadView1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SpreadView, defStyleAttr, 0);
        centerColor = typedArray.getColor(R.styleable.SpreadView_wavecolor, Color.parseColor("#666666"));
        mSpace = typedArray.getInteger(R.styleable.SpreadView_padding, 100);
        mWidth = typedArray.getInteger(R.styleable.SpreadView_width, 400);
        typedArray.recycle();
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        alpharate = 255f / mWidth; //注意这里 如果为int类型就会为0,除数中f一定要加,默认int ;
        mAlphas.add(255);
        widths.add(0f);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(centerColor);
        for (int i = 0; i < widths.size(); i++) { //遍历圆数目
            Integer cuAlpha = mAlphas.get(i);
            mPaint.setAlpha(cuAlpha);
            Float aFloat = widths.get(i);
            canvas.drawCircle(getWidth() >> 1, getHeight() >> 1, aFloat, mPaint); //画圆
            if (isStarting && aFloat < mWidth) {  //扩散直径和透明度
                mAlphas.set(i, (int) (255 - alpharate * aFloat));
                widths.set(i, aFloat + 1);
            }

        }

        if (isStarting && widths.size() >= 10) {
            mAlphas.remove(0);
            widths.remove(0);
        }

        if (isStarting &&  widths.get(widths.size() - 1) == mSpace) {
            mAlphas.add(255);
            widths.add(0f);
        }

        invalidate();

    }
    // 执行动画
    public void start() {
        isStarting = true;
    }

    // 停止动画
    public void stop() {
        isStarting = false;
    }

    // 判断是都在不在执行
    public boolean isStarting() {
        return isStarting;
    }

}
